package com.lambert.young.im.handler;

import com.alibaba.fastjson2.JSON;
import com.lambert.young.im.IMServer;
import com.lambert.young.im.command.Result;
import com.lambert.young.im.command.ResultType;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

/**
 * <p>TODO </p>
 *
 * @author liubaolin
 * @since 2024/11/1 10:55 AM
 */
public class OnlineUsersHandler {
    public static void execute(ChannelHandlerContext ctx){
        IMServer.USERS.values().removeIf(channel -> {
            if (channel instanceof Channel chat){
                return !chat.isActive();
            }
            return false;
        });
        ctx.channel().writeAndFlush(Result.success(ResultType.GET_ONLINE_USERS, JSON.toJSONString(IMServer.USERS.keySet())));
    }
}
